using System.ComponentModel;
using System.Data;
using MySqlConnector;

namespace Front._1118;

public partial class addBookForm : Form
{
    public addBookForm()
    {
        InitializeComponent();
    }
    
    // private void btnSave_Click(object sender, EventArgs e)
    // {
    //     // 1. 收集输入的图书信息（书名、作者等）
    //     // 2. 将信息保存到数据库
    //     // 3. 提示新增成功
    //     MessageBox.Show("图书新增成功！");
    //     this.Close(); // 关闭新增界面
    // }
    
    // 新增界面的“取消”按钮点击事件
    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void addBookForm_Load(object sender, EventArgs e)
    {
        Init();
    }

    public void Init()
    {
        //加载出版社信息
        GetAllPublishers();

        //加载分类信息
        //加载书籍信息
    }

    private void GetAllPublishers()
    {
        // 新增这 2 行！替换成你代码里的「可疑对象」（比如 conn、reader、cboPublisher）
        // MessageBox.Show($"下拉框是否为 null：{cboPublisher == null}");
        // MessageBox.Show($"数据库连接是否为 null：{conn == null}"); // 如果你代码里有 conn 变量
        
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();

        string sql = "SELECT publisher_id,publisher_name FROM publisher_info ";
        //创建MysqlDataAdapter
        MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sql, connection);
        //创建DataSet
        DataSet dataSet = new DataSet();
        //填充数据
        mySqlDataAdapter.Fill(dataSet, "publisher_info");
        // this.dataGridView1.DataSource = dataSet.Tables["book_info"];
        if (dataSet.Tables["publisher_info"].Rows.Count > 0)
        {
            DataTable publisherTable = dataSet.Tables["publisher_info"];
            // 插入一行全部
            DataRow row = publisherTable.NewRow();
            row[0] = "0";
            row[1] = "全部";
            publisherTable.Rows.InsertAt(row, 0);

            publisher.DataSource = publisherTable;
            publisher.ValueMember = publisherTable.Columns[0].ColumnName;
            publisher.DisplayMember = publisherTable.Columns[1].ColumnName;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //获取组件的值
        string bookName = this.book_name.Text;
        string isbn = this.isbn.Text;
        //获取所选出版社编号
        int publisherId = this.publisher.SelectedIndex;
            
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();

        String sql = "insert  into `book_info`(`isbn`,`book_name`,`author`,`publisher_id`,`publish_date`,`book_category`,`book_price`,`book_pages`,`book_desc`,`book_status`,`shelf_id`,`purchase_date`,`book_version`,`create_time`) values \n(@isbn,@book_name,'刘增杰',@publisher_id,'2021-06-01','计算机科学',59.80,320,'讲解MySQL基础与实战开发',1,1,'2023-01-10','第2版','2025-10-10 10:11:29')";

        using var command = new MySqlCommand(sql, connection);
        //给sql 变量填充值
        command.Parameters.AddWithValue("@isbn", isbn);
        command.Parameters.AddWithValue("@book_name",bookName);
        command.Parameters.AddWithValue("@publisher_id", publisherId);
        int count = command.ExecuteNonQuery();
        if (count > 0)
        {
            //添加成功
            MessageBox.Show("添加成功");
            this.DialogResult = DialogResult.OK;
            //关闭窗体
            this.Close();
        }
        else
        {
            MessageBox.Show("添加失败");
            this.DialogResult = DialogResult.Cancel;
            //关闭窗体
            this.Close();
        }
    }


    private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        
    }
}